<?php
class UserMapper{
    protected $table;
    protected $query;

    public function setTable($_table){
        $this->table = $_table;
        $this->query = 'SET NAMES utf8; SET CHARACTER SET utf8';
        return $this;
    }
    public function getTable(){
        return $this->table;
    }

    public function save($user){
        $arr = array(
            'username'  => $user->getUsername(),
            'password'  => $user->getPassword(),
            'salt'      => $user->getSalt(),
            'role'      => $user->getRole(),
            'karma'     => $user->getKarma(),
            'firstname' => $user->getFirstname(),
            'lastname'  => $user->getLastname(),
            'age'       => $user->getAge(),
            'sex'       => $user->getSex(),
            'avatar'    => $user->getAvatar(),
            'active'    => $user->getActive()
        );
        $db = Database::getInstance();
        //$db = $reg->get('db');
        if(null == ($id = $user->getId())){
            $db->insert('users', $arr);
        }else{
            $db->update('users', $arr, $id);
        }
    }

    public function find($id, $user){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $data = $db->select('users', $id);
        $user   ->setUsername($data['username'])
                ->setPassword($data['password'])
                ->setId($id)
                ->setFirstname($data['firstname'])
                ->setLastname($data['lastname'])
                ->setAge($data['age'])
                ->setSex($data['sex'])
                ->setAvatar($data['avatar'])
                ->setMapper($this)
                ->setSalt($data['salt'])
                ->setActive($data['active'])
                ->setKarma($data['karma'])
                ->setRole($data['role']);
                
//        var_dump($m);
//        exit;
    }

    public function delete($user){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $db->delete('users', $user->getId());
    }

    public function isUser($_username){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $where = '';
        $where .= 'username = "' . $_username . '"';
        $res = $db->selectWhere('users', '*' , $where);
        $rezult = count($res);
        if($rezult > 0){
            return $res;
        }else{
            return false;
        }
    }
    public function fetchAllLittleUsers(){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $resultSet = $db->fetchAll('users');
        //var_dump($resultSet);
        //exit;

        //$resultSet = $->fetchAllLittleUsers();
        $users = array();
        foreach($resultSet as $one){
            $user = new UserModel();
            $user   ->setUsername($one['username'])
                    ->setPassword($one['password'])
                    ->setRole($one['role'])
                    ->setId($one['id'])
                    ->setFirstname($one['firstname'])
                    ->setLastname($one['lastname'])
                    ->setAge($one['age'])
                    ->setSex($one['sex'])
                    ->setAvatar($one['avatar'])
                    ->setActive($one['active'])
                    ->setKarma($one['karma'])
                    ->setMapper($this);
            $users[] = $user;
        }
        return $users;
    }

    public static function getCurrentUser(){
        if(empty($_SESSION)){
            return new UserModel();
            
        }else{
            $ses_id = $_SESSION['id'];
            $curuser = new UserModel();
            $user = $curuser->find($ses_id);
            return $user;
        }
    }

    public function fetchUsersByFilter($str){
        $db = Database::getInstance();
        //$db = $reg->get('db');
        $resultSet = $db->selectWhere('users', 'id', $str);

        $users = array();
        foreach($resultSet as $one){
            $user = new UserModel();
            $user   ->setId($one['id'])
                    ->setMapper($this);
            $users[] = $user;
        }
        return $users;
    }
}
?>
